export default class BackAudio {
  constructor(options) {
    let prama = options || {}
    this.backgroundAudioManager = wx.getBackgroundAudioManager();
    this.init(prama);
    this.isPlay = true;
    this.src = prama.src;
    return this
  }
  destroy() {
    this.backgroundAudioManager.destroy()
  }
  play() {
    console.log("src - "+this.src);

    if (!this.isPlay) return
    if (this.src)
    {
      this.backgroundAudioManager.play();
      return
    }
    this.src = src;
    this.backgroundAudioManager.src = src;
    this.backgroundAudioManager.play();

    return this
  }
  stop() {
    this.backgroundAudioManager.stop();
    //this.onMusicTapStop();
    this.isPlay = true;
    return this
  }
  init(prama) {
    //this.backgroundAudioManager.autoplay = prama.autoplay || false;//默认为不自动播放
    //this.backgroundAudioManager.obeyMuteSwitch = false;
    //this.backgroundAudioManager.mixWithOther = true;
    backgroundAudioManager.title = '天圆地方';
    backgroundAudioManager.epname = '天圆地方';
    backgroundAudioManager.singer = '纸谜';
    backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000';

    if (prama.src) {
      this.src = prama.src;
      this.backgroundAudioManager.src = prama.src
    }
    //停止播放
    this.backgroundAudioManager.onStop(() => {
      this.isPlay = true;

      if (prama.onStop) {
        prama.onStop()
      }
      console.log('停止播放')
    })
    //开始播放事件
    this.backgroundAudioManager.onPlay(() => {
      this.isPlay = false;

      if (prama.onPlay) {
        prama.onPlay()
      }
      console.log('开始播放3333')
    })
    //音频自然播放结束事件
    this.backgroundAudioManager.onEnded(() => {
      this.isPlay = true;
      console.log('停止播放')
      //音频自然播放结束事件
      if (prama.onEnded) {
        prama.onEnded()
      }
    })
  }

  onMusicTap () {
    console.log("播放背景音乐");
    console.log("src - "+this.src);
    this.isPlay = false;
    //if (!this.isPlay) return
    //this.src = src;
    const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.title = '天圆地方';
    backgroundAudioManager.epname = '天圆地方';
    backgroundAudioManager.singer = '纸谜';
    backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000';
    // 设置了 src 之后会自动播放
    //backgroundAudioManager.src = 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/audio.mp3';
    backgroundAudioManager.src = this.src;//this.data.play_beijing_music_audio,
    backgroundAudioManager.play();
    backgroundAudioManager.onPlay(() => {
      console.log("音乐播放开始");
      this.isPlay = false;
    })
    backgroundAudioManager.onEnded(() => {
      console.log("音乐播放结束");
      //this.onMusicTap();
      //this.onMusicTapStop();
      this.isPlay = true;

    })


    //this.audio6.play(this.data.play_beijing_music_audio) //调用播放方法
    
  }

  onMusicTapStop (){
    console.log("停止背景音乐");
    const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.stop()
    this.isPlay = true;

  }


}